<?php

class BackOffice extends Controller {

	private $_newsmanager;
	private $_tnews;
	private $_abilitiesmanager;
	private $_tabilities;
	private $_productsmanager;
	private $_tproducts;
	private $_productsaddsmanager;
	private $_tproductsadds;

    public function __construct() {
	
		$user = new User();
		$checkuser = $user->checkUser();
		if ($checkuser == false) {
			header('Location: index.php');
			exit();
		}
		//Define Default template
        $this->setTplFile(PATH_TPL . '/backoffice/Index.tpl.php');
		//Get datas
		$this->_newsmanager = new NewsModel();
		$this->_tnews = $this->_newsmanager->getList('full');
		$this->_abilitiesmanager = new AbilitiesModel();
		$this->_tabilities = $this->_abilitiesmanager->getList('full');
		$this->_productsmanager = new ProductsModel();
		$this->_tproducts = $this->_productsmanager->getList('full');
		$this->_productsaddsmanager = new ProductsAddsModel();
		$this->_tproductsadds = $this->_productsaddsmanager->getList();
		
    }
	
	public function tnews() {
		return $this->_tnews;
	}
	
	public function tabilities() {
		return $this->_tabilities;
	}
	
	public function tproducts() {
		return $this->_tproducts;
	}
	
	public function tproductsadds() {
		return $this->_tproductsadds;
	}
	
	public function clearMethod() {
		
		$this->setIsAjax(false);
		
		if (isset($_GET['module'])) {
			$module = $_GET['module'];
			$tpl = PATH_TPL . 'backoffice/' . ucfirst($module) . '.tpl.php';
			$this->setModuleTplFile($tpl);
		}
		
	}
	
	public function ajaxMethod() {
		
		$this->setIsAjax(true);
		
		if (isset($_POST['module'])) {
			$module = $_POST['module'];
			$action = $_POST['action'];
			$manager = '_'.$module.'manager';
			switch ($action) {
				case 'create':
					Token::checkToken();
					$check = $this->$manager->addObj();
					$this->addAjaxDatas('returnDatas', $check);
					break;
				case 'delete':
					Token::checkToken();
					$check = $this->$manager->delObj($_POST['id']);
					$this->addAjaxDatas('returnDatas', $check);
					break;
				case 'update':
					Token::checkToken();
					$check = $this->$manager->upObj($_POST['form']);
					$this->addAjaxDatas('returnDatas', $check);
					break;
				case 'deletegroup':
					Token::checkToken();
					$check = $this->$manager->delGroup($_POST['group']);
					$this->addAjaxDatas('returnDatas', $check);
					break;
				case 'updateall':
					Token::checkToken();
					$check = $this->$manager->upAll($_POST['formArray']);
					$this->addAjaxDatas('returnDatas', $check);
					break;
				case 'reload':
					$tpl = PATH_TPL . 'backoffice/' . ucfirst($module) . '.tpl.php';
					$this->setModuleTplFile($tpl);
					$this->addAjaxDatas('view', $this->getViewContent());
					break;
			}
		}
		
		if (isset($_SERVER['HTTP_X_FILE_NAME'])) {
			$filename = $_SERVER['HTTP_X_FILE_NAME'];
			$id = $_SERVER['HTTP_X_ID'];
			$dir = $_SERVER['HTTP_X_DIR'];
			$content = file_get_contents('php://input');
			$this->_newsmanager->upload($id, $dir, $filename, $content);
		}
		
		if (isset($_POST['deleteimg']) && $_POST['deleteimg'] == true) {
			Token::checkToken();
			$check = Upload::deleteThumbs($_POST['imglink']);
			$this->addAjaxDatas('returnDatas', $check);
		}
		
	}

}

?>